﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;

namespace QuanLyKho.DKChung
{
    public partial class DKChung_DkKhoPopup : Form
    {
        public static string MaID = "0";
        public bool update = false;

        public DKChung_DkKhoPopup()
        {
            InitializeComponent();
            LoadData();
        }

        public void LoadData()
        {
            if (MaID != "0")
            {
                var data = DataAll.SelectAll("DkChung_Kho", "*", " WHERE IDKho=" + MaID);
                txtMaKho.Text = data.Rows[0]["MaKho"].ToString();
                txtTenKho.Text = data.Rows[0]["TenKho"].ToString();
                dtNgayTao.Value = Convert.ToDateTime(data.Rows[0]["NgayTao"].ToString());
                txtMoTa.Text = data.Rows[0]["MoTa"].ToString();
            }
        }

        public void ResetControl()
        {
            txtMaKho.Text = string.Empty;
            txtTenKho.Text = string.Empty;
            txtMoTa.Text = string.Empty;
        }

        private void bntLuuLai_Click(object sender, EventArgs e)
        {
            if (MaID != "0")
            {
                string message = DataAll.Update("DkChung_Kho", "MaKho=N'" + txtMaKho.Text + "', TenKho=N'" + txtTenKho.Text
                    + "', NgayTao='" + dtNgayTao.Value.ToString("MM/dd/yyyy") + "',MoTa=N'" + txtMoTa.Text + "' WHERE IDKho=" + MaID);
                MessageBox.Show(message.Equals("success") ? "Sửa kho thành công" : message, "Message",
                                MessageBoxButtons.OK,
                                message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
            }
            else
            {
                string message = DataAll.InsertAll("DkChung_Kho", txtMaKho.Text + "," + txtTenKho.Text + "," + dtNgayTao.Value.ToString("MM/dd/yyyy")
                + "," + txtMoTa.Text);
                MessageBox.Show(message.Equals("success") ? "Thêm kho thành công" : message, "Message",
                                MessageBoxButtons.OK,
                                message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                if (message.Equals("success"))
                {
                    ResetControl();
                }
            }
        }

        private void bntThoat_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
